package com.example.dadong.checkwordview;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;


import com.lancoo.evaluate.api.EvaluateContext;
import com.lancoo.evaluate.api.OnEvaluatedCallback;
import com.lancoo.evaluate.bean.Evaluate;
import com.lancoo.evaluate.bean.EvaluateResult;
import com.tt.CoreType;
import com.tt.QType;
import com.tt.lancoo.OnRecordCallback;

import java.io.File;
import java.util.ArrayList;

public class EvaluateActivity extends AppCompatActivity implements OnTouchListener {

    private TextView mTextView;
    private EvaluateContext mEvaluateContext;
    private Evaluate mEvaluate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evaluate);
        initData();
        initView();
    }

    private void initData() {
        mEvaluateContext = new EvaluateContext(this, "LiJiJun");

        mEvaluate = new Evaluate();
        mEvaluate.setCoreType(CoreType.EN_WORD_EVAL);
        mEvaluate.setQType(QType.QTYPE_EMPTY);
        mEvaluate.setRefText("Happy");
        mEvaluate.setTotalScore(20);
    }

    private void initView() {
        mTextView = (TextView) findViewById(R.id.tv);
        findViewById(R.id.recordBtn).setOnTouchListener(this);
        findViewById(R.id.playbackButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    public void onEvaluate() {
        ArrayList<Evaluate> evaluates = new ArrayList<>();
        evaluates.add(mEvaluate);
        mEvaluateContext.setOnEvaluatedCallback(new OnEvaluatedCallback() {
            @Override
            public void onEvaluated(ArrayList<EvaluateResult> evaluateResults) {
                EvaluateResult evaluateResult = evaluateResults.get(0);
                mTextView.setText(evaluateResult.getScore() + "");
            }
        });
        mEvaluateContext.startEvaluate(evaluates);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                String path = Environment.getExternalStorageDirectory().getAbsolutePath()
                        + File.separator + "record.wav";
                File wav = new File(path);
                if (wav.exists()) {
                    wav.delete();
                } else {
                    wav.mkdir();
                }

                mEvaluateContext.startRecord(
                        wav,
                        new OnRecordCallback() {
                            @Override
                            public void onRecordStart() {
                                Log.e("My", "onRecordStart================");
                            }

                            @Override
                            public void onRecordEnd(String wavPath, String pcmPath) {
                                mEvaluate.setWavPath(wavPath);
                                mEvaluate.setPcmPath(pcmPath);
                                Log.e("My", wavPath + "<================>" + pcmPath);
                            }

                            @Override
                            public void onFailure(String msg) {
                                Log.e("My", "onFailure================>" + msg);
                            }
                        }
                );
                mTextView.setText("录音中。。。");
                return true;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                mEvaluateContext.stopRecord();
                mTextView.setText("录音结束");
                onEvaluate();
                return true;
        }
        return false;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        mEvaluateContext.onRequestPermissionsResult(requestCode, permissions, grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mEvaluateContext.onDestroy();
    }
}
